package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.vo.StationVo;
import com.ruoyi.common.utils.DateUtils;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.StationMapper;
import com.ruoyi.business.domain.Station;
import com.ruoyi.business.service.IStationService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 车站管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class StationServiceImpl implements IStationService 
{
    @Autowired
    private StationMapper stationMapper;

    /**
     * 查询车站管理
     * 
     * @param id 车站管理主键
     * @return 车站管理
     */
    @Override
    public Station selectStationById(Long id)
    {
        return stationMapper.selectStationById(id);
    }

    /**
     * 查询车站管理列表
     * 
     * @param station 车站管理
     * @return 车站管理
     */
    @Override
    public List<Station> selectStationList(Station station)
    {
        return stationMapper.selectStationList(station);
    }

    /**
     * 新增车站管理
     * 
     * @param station 车站管理
     * @return 结果
     */
    @Override
    public int insertStation(Station station)
    {
        station.setCreateTime(DateUtils.getNowDate());
        return stationMapper.insertStation(station);
    }

    /**
     * 修改车站管理
     * 
     * @param station 车站管理
     * @return 结果
     */
    @Override
    public int updateStation(Station station)
    {
        station.setUpdateTime(DateUtils.getNowDate());
        return stationMapper.updateStation(station);
    }

    /**
     * 批量删除车站管理
     * 
     * @param ids 需要删除的车站管理主键
     * @return 结果
     */
    @Override
    public int deleteStationByIds(Long[] ids)
    {
        return stationMapper.deleteStationByIds(ids);
    }

    /**
     * 删除车站管理信息
     * 
     * @param id 车站管理主键
     * @return 结果
     */
    @Override
    public int deleteStationById(Long id)
    {
        return stationMapper.deleteStationById(id);
    }
    /**
     * 批量新增车站管理
     * @param stationList
     * @return 结果
     */
    @Override
    public int insertStations(List<Station> stationList) {
        return stationMapper.insertStations(stationList);
    }

    public List<Station> selectStationName(String name) {
        return stationMapper.selectStationName(name);
    }

    public List<Station> selectLimit(){
        return stationMapper.selectLimit();
    }
    /**
     * 批量单查城市车站管理
     * @param name
     * @return 结果
     */
    @Override
    public Station selectStationByName(String name) {
        return stationMapper.selectStationByName(name);
    }

    /**
     * 下载城市导入模板 - 不包含错误字段
     */
    @Override
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        // 创建自定义的工作簿和工作表
        SXSSFWorkbook wb = new SXSSFWorkbook(500);
        try {
            Sheet sheet = wb.createSheet("城市数据");
            
            // 创建表头行并设置样式
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setBorderBottom(BorderStyle.THIN);
            headerStyle.setBorderLeft(BorderStyle.THIN);
            headerStyle.setBorderRight(BorderStyle.THIN);
            headerStyle.setBorderTop(BorderStyle.THIN);
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            
            Font headerFont = wb.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);
            
            // 仅设置业务相关列，不包含错误行号和错误原因
            String[] headers = {"id", "城市", "城市拼音", "城市拼音首字母"};
            for (int i = 0; i < headers.length; i++) {
                Cell headerCell = headerRow.createCell(i);
                headerCell.setCellValue(headers[i]);
                headerCell.setCellStyle(headerStyle);
                sheet.setColumnWidth(i, 5000); // 设置合适的列宽
            }
            
            // 创建数据行样式
            CellStyle dataStyle = wb.createCellStyle();
            dataStyle.setBorderBottom(BorderStyle.THIN);
            dataStyle.setBorderLeft(BorderStyle.THIN);
            dataStyle.setBorderRight(BorderStyle.THIN);
            dataStyle.setBorderTop(BorderStyle.THIN);
            
            // 添加示例数据
            Row exampleRow = sheet.createRow(1);
            
            // 输出到响应
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = "station_template.xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            
            wb.write(response.getOutputStream());
        } finally {
            wb.dispose();
        }
    }

    /**
     * 导出导入错误数据 - 显示ID和错误原因
     */
    @Override
    public void exportErrorData(HttpServletResponse response, List<StationVo> errorDataList) throws IOException {
        // 创建自定义的工作簿和工作表
        SXSSFWorkbook wb = new SXSSFWorkbook(500);
        try {
            Sheet sheet = wb.createSheet("城市数据");
            
            // 创建表头行并设置样式
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setBorderBottom(BorderStyle.THIN);
            headerStyle.setBorderLeft(BorderStyle.THIN);
            headerStyle.setBorderRight(BorderStyle.THIN);
            headerStyle.setBorderTop(BorderStyle.THIN);
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            
            Font headerFont = wb.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);
            
            // 设置列，包含ID和错误原因
            String[] headers = {"ID", "城市", "城市拼音", "城市拼音首字母", "错误原因"};
            for (int i = 0; i < headers.length; i++) {
                Cell headerCell = headerRow.createCell(i);
                headerCell.setCellValue(headers[i]);
                headerCell.setCellStyle(headerStyle);
                sheet.setColumnWidth(i, 6000); // 设置合适的列宽
            }
            
            // 创建数据行样式
            CellStyle dataStyle = wb.createCellStyle();
            dataStyle.setBorderBottom(BorderStyle.THIN);
            dataStyle.setBorderLeft(BorderStyle.THIN);
            dataStyle.setBorderRight(BorderStyle.THIN);
            dataStyle.setBorderTop(BorderStyle.THIN);
            
            // 如果没有数据，添加一条提示
            if (errorDataList == null || errorDataList.isEmpty()) {
                Row dataRow = sheet.createRow(1);
                Cell cell = dataRow.createCell(0);
                cell.setCellValue("没有错误数据");
                cell.setCellStyle(dataStyle);
                sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 4));
            } else {
                // 填充数据行
                int rowIndex = 1;
                for (StationVo station : errorDataList) {
                    Row dataRow = sheet.createRow(rowIndex++);
                    
                    // ID列 - 使用getId()而不是getRowNum()
                    Cell cell0 = dataRow.createCell(0);
                    String idValue = station.getId() != null ? station.getId().toString() : "";
                    cell0.setCellValue(idValue);
                    cell0.setCellStyle(dataStyle);
                    
                    // 城市名称
                    Cell cell1 = dataRow.createCell(1);
                    cell1.setCellValue(station.getName() != null ? station.getName() : "");
                    cell1.setCellStyle(dataStyle);
                    
                    // 城市拼音
                    Cell cell2 = dataRow.createCell(2);
                    cell2.setCellValue(station.getNamePinyin() != null ? station.getNamePinyin() : "");
                    cell2.setCellStyle(dataStyle);
                    
                    // 城市首字母
                    Cell cell3 = dataRow.createCell(3);
                    cell3.setCellValue(station.getNamePy() != null ? station.getNamePy() : "");
                    cell3.setCellStyle(dataStyle);
                    
                    // 错误原因
                    Cell cell4 = dataRow.createCell(4);
                    cell4.setCellValue(station.getErrorMsg() != null ? station.getErrorMsg() : "");
                    cell4.setCellStyle(dataStyle);
                }
            }
            
            // 输出到响应
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = "城市导入错误数据_" + System.currentTimeMillis() + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            
            wb.write(response.getOutputStream());
        } finally {
            wb.dispose();
        }
    }

    /**
     * 清空所有城市数据
     * 
     * @return 结果
     */
    @Override
    public int clearStation()
    {
        return stationMapper.clearStation();
    }
}
